package algotithm.leetcode.dfs.test90;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

/**
 * @author zhouyanxiang
 * @Date 2021-03-2021/3/31-10:15
 * @Title 90. 子集 II
 */
public class Solution {

    public static void main(String[] args) {
        Solution solution = new Solution();
        int[] nums = {1,2,2};
        List<List<Integer>> lists = solution.subsetsWithDup(nums);
        for (List<Integer> list : lists) {
            System.out.println(list);
        }
    }

    public List<List<Integer>> subsetsWithDup(int[] nums) {
        List<List<Integer>> res = new ArrayList<>();
        LinkedList<Integer> list = new LinkedList<>();
        int n = nums.length;
        if (n == 0) {
            return res;
        }
        Arrays.sort(nums);
        dfs(res,list,0,n,nums);
        return res;
    }

    public  void dfs(List<List<Integer>> res,LinkedList<Integer> list,int start,int len,int[] nums) {
        res.add(new ArrayList<>(list));
        for (int i = start; i < len; i++) {
            if (i > start && nums[i] == nums[i - 1]) {
                continue;
            }
            list.add(nums[i]);
            dfs(res,list,i+1,len,nums);
            list.removeLast();
        }
    }
}
